package com.scs.application.core.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scs.application.core.entity.BaseEntity;
import com.scs.application.core.query.QueryContext;
import com.scs.application.core.utils.UtilCommon;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Collectors;

import static com.scs.application.core.consts.Const.FILE_STORE_SRC;

/**
 * @Description:操作JSON文件服务接口
 */
public interface IBaseFileService<T extends BaseEntity, D> {
    /**
     * Page接口实现
     *
     * @param context
     * @param list
     * @return
     */
    default Page<T> filePage(QueryContext context, List<T> list) {
        Page<T> page = new Page<>();
        if (list == null || list.isEmpty()) {
            return page;
        }
        if (context.getPage() != null) {
            Page currentPage = context.getPage();
            page.setSize(currentPage.getSize())
                    .setCurrent(currentPage.getCurrent())
                    .setRecords(list.stream().skip(((currentPage.getCurrent() - 1) * currentPage.getSize())).limit(currentPage.getSize()).collect(Collectors.toList()))
                    .setTotal(list.size());
        }
        return page;
    }

    /**
     * 获取JSON数据
     *
     * @param src 文件路径
     * @return
     */
    default List<D> getDTOList(String src) {
        List<D> list = new ArrayList<>();
        String protocol = this.getClass().getClassLoader().getResource("").getProtocol();
        if (protocol.startsWith("jar")) {
            // jar包方式启动
            JarFile localJarFile;
            try {
                String path = this.getClass().getClassLoader().getResource("static/").getPath();
                String base = UtilCommon.isLinux() ? "file:" : "file:\\";
                path = path.substring(base.length());
                String jarPath = path.substring(0, path.indexOf(".jar") + 4);
                localJarFile = new JarFile(jarPath);
                Enumeration<JarEntry> entries = localJarFile.entries();
                while (entries.hasMoreElements()) {
                    JarEntry jarEntry = entries.nextElement();
                    String name = jarEntry.getName();
                    if (name.indexOf("static/file-store/" + src) == -1 || name.endsWith("static/file-store/" + src)) {
                        continue;
                    }
                    String fileName = name.substring(name.lastIndexOf("/") + 1);
                    D entity = getDTO(src + fileName);
                    if (entity != null) {
                        list.add(entity);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        } else {
            // 文件方式启动
            File path = new File(FILE_STORE_SRC + src);
            if (!path.exists()) {
                path.mkdirs();
            }
            File[] files = path.listFiles();
            for (File file : files) {
                String fileName = file.getName();
                D entity = getDTO(src + fileName);
                if (entity != null) {
                    list.add(entity);
                }
            }
        }
        return list;
    }

    /**
     * 根据JSON获取 DTO
     *
     * @param fileSrc
     * @return
     */
    default D getDTO(String fileSrc) {
        return null;
    }
}
